//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//
package com.kotlin.demo.tools

import java.util.concurrent.ArrayBlockingQueue
import java.util.concurrent.Executors
import java.util.concurrent.ThreadPoolExecutor
import java.util.concurrent.ThreadPoolExecutor.AbortPolicy
import java.util.concurrent.TimeUnit

class ThreadPoolManager private constructor() {
    fun execute(r: Runnable) {
        executor.execute(r)
        println("当前线程池长度：" + executor.activeCount)
    }

    fun cancel(r: Runnable?) {
        if (r != null) {
            executor.queue.remove(r)
        }
    }

    fun remove(r: Runnable?) {
        if (r != null) {
            executor.remove(r)
        }
    }

    val isThreadShutdown: Boolean
        get() = executor.isShutdown

    fun shutDown() {
        executor.shutdown()
    }

    val isTerminated: Boolean
        get() = executor.isTerminated

    companion object {
        private val CPU_COUNT = Runtime.getRuntime().availableProcessors()
        private val CORE_POOL_SIZE = CPU_COUNT + 1
        private val MAXIMUM_POOL_SIZE = CPU_COUNT * 2 + 1
        private const val KEEP_ALIVE = 1
        private var sInstance: ThreadPoolManager? = null
        private var executor = ThreadPoolExecutor(
            CORE_POOL_SIZE,
            MAXIMUM_POOL_SIZE,
            1L,
            TimeUnit.SECONDS,
            ArrayBlockingQueue(20),
            Executors.defaultThreadFactory(),
            AbortPolicy()
        )

        @Synchronized
        fun getsInstance(): ThreadPoolManager {
            if (sInstance == null) {
                sInstance = ThreadPoolManager()
            }

            return sInstance!!
        }
    }
}
